/**
 * thinkjs model 万能数据调用标签
 * {% model data ="data"%}
 *
 * data:接受返回数据的变量名称，例: data = "data"
 * table:要查询的主表比如 table = "uuu"
 * join :{Object} 要组合的查询语句，默认为 LEFT JOIN
 * field {String} 设置要查询的字段，必须是字符串
 * fieldReverse:{String} 反选字段，即查询的时候不包含这些字段
 * alias:{String} 表别名
 * limit(offset, length) :offset {Number} 设置查询的起始位置 length {Number} 设置查询的数据长度
 * where(where):where {Object} where 条件
 * order {String} 排序方式
 * cache {Number} 缓存有效时间，单位为秒,建议1000秒
 */
global.model = function() {
	this.tags = ['model'];
	this.parse = function(parser, nodes, lexer) {
		var tok = parser.nextToken();
		var args = parser.parseSignature(null, true);
		parser.advanceAfterBlockEnd(tok.value);
		return new nodes.CallExtensionAsync(this, 'run', args);
	};
	this.run = async function(context, args, callback) {
		// console.log(args);
		const data = think.isEmpty(args.data) ? 'data' : args.data;
		const table = think.isEmpty(args.table) ? '5' : args.table;
		const join = think.isEmpty(args.join) ? false : JSON.parse(args.join);
		const fieldReverse = think.isEmpty(args.fieldReverse) ? false : args.fieldReverse;
		const alias = think.isEmpty(args.alias) ? false : args.alias;
		const limit = think.isEmpty(args.limit) ? false : args.limit;
		const page = think.isEmpty(args.page) ? false : args.page;
		console.log(args.where)
		const where = think.isEmpty(args.where) ? false : JSON.parse(args.where);
		const order = think.isEmpty(args.order) ? false : args.order;
		const cache = think.isEmpty(args.cache) ? false : Number(args.cache) * 1000;
		const field = think.isEmpty(args.field) ? false : args.field;
		const query = think.isEmpty(args.query) ? false : args.query;
		const model = think.model(table);
		if(args.includesubcategory == 1){
			
		}
		// console.log(cache);
		// 缓存
		if(cache) {
			model.cache(cache);
		}
		// 表别名
		if(alias) {
			model.alias(alias);
		}
		// where
		if(where) {
			model.where(where);
		}
		// order
		if(order) {
			model.order(order);
		}
		// 查询的字段
		if(field) {
			model.field(field);
		}
		// 排除字段
		if(fieldReverse) {
			model.fieldReverse(fieldReverse);
		}
		// 查询条数
		if(limit) {
			if(limit.split(',').length === 2) {
				model.limit(limit.split(',')[0], limit.split(',')[1]);
			} else {
				model.limit(limit);
			}
		}
		if(page) {
			if(page.split(',').length === 2) {
				model.page(page.split(',')[0], page.split(',')[1]);
			} else {
				model.page(page);
			}
		}
		// join查询c
		if(join) {
			model.join(join);
		}
		let ret = "";
		if(query == 'find') {
			ret = await model.find();
		} else if(query == 'countSelect') {
			ret = await model.countSelect();
		}
		else {
			ret = await model.select();
		}
		console.log(ret)
		// console.log(ret);
		context.ctx[data] = ret;
		return callback(null, '');
	};
};
global.appModel = function() {
	this.tags = ['appModel'];
	this.parse = function(parser, nodes, lexer) {
		var tok = parser.nextToken();
		var args = parser.parseSignature(null, true);
		parser.advanceAfterBlockEnd(tok.value);
		return new nodes.CallExtensionAsync(this, 'run', args);
	};
	this.run = async function(context, args, callback) {
		// console.log(args);
		const data = think.isEmpty(args.data) ? 'data' : args.data;
		const table = args.table.split("/");
		const join = think.isEmpty(args.join) ? false : JSON.parse(args.join);
		const fieldReverse = think.isEmpty(args.fieldReverse) ? false : args.fieldReverse;
		const alias = think.isEmpty(args.alias) ? false : args.alias;
		const limit = think.isEmpty(args.limit) ? false : args.limit;
		console.log(args.where)
		const where = think.isEmpty(args.where) ? false : JSON.parse(args.where);
		const order = think.isEmpty(args.order) ? false : args.order;
		const cache = think.isEmpty(args.cache) ? false : Number(args.cache) * 1000;
		const field = think.isEmpty(args.field) ? false : args.field;
		const model = think.appModel(table[1], table[0]);
		// console.log(cache);
		// 缓存
		if(cache) {
			model.cache(cache);
		}
		// 表别名
		if(alias) {
			model.alias(alias);
		}
		// where
		if(where) {
			model.where(where);
		}
		// order
		if(order) {
			model.order(order);
		}
		// 查询的字段
		if(field) {
			model.field(field);
		}
		// 排除字段
		if(fieldReverse) {
			model.fieldReverse(fieldReverse);
		}
		// 查询条数
		if(limit) {
			if(limit.split(',').length === 2) {
				model.limit(limit.split(',')[0], limit.split(',')[1]);
			} else {
				model.limit(limit);
			}
		}
		// join查询c
		if(join) {
			model.join(join);
		}
		const ret = await model.select();
		console.log(ret)
		// console.log(ret);
		context.ctx[data] = ret;
		return callback(null, '');
	};
};